package br.com.web.panificadora.DAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.web.panificadora.beans.Login;
import br.com.web.panificadora.repositorio.LoginRepositorio;

@Component
public class LoginDAO extends HibernateBaseDao<Login> implements LoginRepositorio{
	
	private SessionFactory factory;

	@Autowired
	public LoginDAO(SessionFactory factory) {
		super(Login.class, factory);
		this.factory = factory;
	}
	
	

	@SuppressWarnings("unchecked")
	public List<Login> list() {
		return factory.openSession().createCriteria(Login.class).list(); 
	}

	public Login findByUsuario(String usuario) {
		Query query = getSession().createQuery("from Login u where u.usuario = :usuario");
		query.setParameter("usuario", usuario);
		
		return (Login) query.uniqueResult();
	}
	
	private Session getSession(){
		return this.factory.openSession();
	}
	
	

}
